{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "4649a4f4-2850-4dcb-9eda-3b73114dbff7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from bs4 import BeautifulSoup\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "352ba39f-36a8-4535-a974-5a5ef97dcd41",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:1: SyntaxWarning: invalid escape sequence '\\l'\n",
      "<>:1: SyntaxWarning: invalid escape sequence '\\l'\n",
      "C:\\Users\\zhenf Wu\\AppData\\Local\\Temp\\ipykernel_17572\\972522094.py:1: SyntaxWarning: invalid escape sequence '\\l'\n",
      "  driver = webdriver.Edge(service= Service('D:\\lishaobai\\prog\\edgedriver_win64\\msedgedriver.exe'))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Telephone: 18362601391\n"
     ]
    }
   ],
   "source": [
    "driver = webdriver.Edge(service= Service('D:\\lishaobai\\prog\\edgedriver_win64\\msedgedriver.exe'))\n",
    "url = 'http://patientview.2m9h.net/Home/Index/MjAyMi8xMC8yMCAwOjAwOjAw'\n",
    "driver.get(url)\n",
    "\n",
    "#VisitNumberKey = 'Y4040786'\n",
    "VisitNumberKey = \"ZA199390\"\n",
    "#VisitNumberKey = \"Y3740899\"\n",
    "patient_number_input = driver.find_element(By.ID, 'VisitNumber')\n",
    "patient_number_input.send_keys(VisitNumberKey)\n",
    "\n",
    "search_button = driver.find_element(By.ID, 'search_Btn')\n",
    "ActionChains(driver).click(search_button).perform()\n",
    "\n",
    "time.sleep(10)\n",
    "\n",
    "html = driver.page_source\n",
    "soup = BeautifulSoup(html, 'html.parser')\n",
    "targets = soup.find_all('a', class_=\"btn default btn-xs purple\")\n",
    "grid = soup.find('div', {'class': 'k-grid-content'})\n",
    "rows = grid.find_all('tr')\n",
    "\n",
    "for row in rows:\n",
    "    record = row.find_all('td')\n",
    "    print(f\"VisitNumberKey: {VisitNumberKey}, Name: {record[11].text.strip()}, Sex: {record[13].text.strip()}, Birth: {record[14].text.strip()}, Telephone: {record[19].text.strip()}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "19ebea80-98b5-42b9-9e4e-e79e6d600aa6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n",
      "VisitNumberKey: ZA199390, Name: 刘伟, Sex: 男, Birth: 1985-12-27, Telephone: 18362601391\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "3e4fffce-2b4d-41e3-9ba9-f9200c15e510",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<td role=\"gridcell\" style=\"text-align:center\"><a class=\"btn default btn-xs purple\" href=\"/Patient/VisitNumber/30348379\" target=\"_blank\"><i class=\"fa fa-edit\"></i> 查看</a> </td>,\n",
       " <td role=\"gridcell\">0638447685</td>,\n",
       " <td role=\"gridcell\">30348379</td>,\n",
       " <td role=\"gridcell\">ZA199390</td>,\n",
       " <td admissiontime=\"true\" role=\"gridcell\">2017-06-23 12:06</td>,\n",
       " <td dischargetime=\"true\" role=\"gridcell\" style=\"display:none\">2017-06-23 12:06</td>,\n",
       " <td dischargetime=\"true\" role=\"gridcell\" style=\"display:none\"></td>,\n",
       " <td role=\"gridcell\">口腔黏膜科  </td>,\n",
       " <td dischargetime=\"true\" role=\"gridcell\" style=\"display: none;\"></td>,\n",
       " <td admissiontime=\"true\" role=\"gridcell\">口腔黏膜科                                   </td>,\n",
       " <td dischargetime=\"true\" role=\"gridcell\" style=\"display: none;\"></td>,\n",
       " <td role=\"gridcell\">刘伟    </td>,\n",
       " <td role=\"gridcell\">本市自费</td>,\n",
       " <td role=\"gridcell\">男</td>,\n",
       " <td role=\"gridcell\">1985-12-27</td>,\n",
       " <td role=\"gridcell\">430921198512******</td>,\n",
       " <td role=\"gridcell\"> </td>,\n",
       " <td role=\"gridcell\">18362601391 </td>,\n",
       " <td role=\"gridcell\"> </td>,\n",
       " <td role=\"gridcell\">18362601391 </td>,\n",
       " <td role=\"gridcell\">普通挂号</td>,\n",
       " <td role=\"gridcell\">就诊结束</td>,\n",
       " <td role=\"gridcell\">自费                          </td>,\n",
       " <td role=\"gridcell\"> </td>,\n",
       " <td role=\"gridcell\"></td>,\n",
       " <td role=\"gridcell\"></td>]"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "record"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
